package me.banxi.ilearnandroid.demo.touch;

import me.banxi.ilearnandroid.demo.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class TouchExampleView extends View {
	private static final String TAG = "TouchExampleView";
	private final Drawable mIcon;
	private float mPosX;
	private float mPosY;
	private float mLastTouchX;
	private float mLastTouchY;
	private static final int INVALID_POINTER_ID = -1;
	/**
	 * The 'active pointer' is the one currently moving our object
	 */
	private int mActivePointerId = INVALID_POINTER_ID;

	public TouchExampleView(Context context) {
		this(context, null, 0);
		Log.d(TAG, "on 1 arg:");
	}

	public TouchExampleView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		Log.d(TAG, "on 2 arg:");
	}

	public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Log.d(TAG, "on 3 arg:");
		mIcon = context.getResources().getDrawable(R.drawable.ic_launcher);
		mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(),
				mIcon.getIntrinsicHeight());

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.d(TAG, "onDraw");
		canvas.save();
		canvas.translate(mPosX, mPosY);
		mIcon.draw(canvas);
		canvas.restore();

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final int action = event.getActionMasked();
		switch (action) {
		case MotionEvent.ACTION_DOWN: {
			final float x = event.getX();
			final float y = event.getY();

			// Remember where we started
			mLastTouchX = x;
			mLastTouchY = y;

			// Save the ID of this pointer
			mActivePointerId = event.getPointerId(0);
			break;
		}
		case MotionEvent.ACTION_MOVE: {
			// Find the index of the active pointer and fetch its position
			final int pointerIndex = event.findPointerIndex(mActivePointerId);

			final float x = event.getX(pointerIndex);
			final float y = event.getY(pointerIndex);
			// Calculate the distance moved
			final float dx = x - mLastTouchX;
			final float dy = y - mLastTouchY;
			// Move the object
			mPosX += dx;
			mPosY += dy;

			// Remember this touch position for the next move event
			mLastTouchX = x;
			mLastTouchY = y;

			// Invalidate to request a redraw
			invalidate();
			break;
		}
		case MotionEvent.ACTION_UP: {
			mActivePointerId = INVALID_POINTER_ID;
			break;
		}
		case MotionEvent.ACTION_CANCEL: {
			mActivePointerId = INVALID_POINTER_ID;
			break;
		}
		case MotionEvent.ACTION_POINTER_UP: {
			// Extract the index of the pointer that left the touch sensor
			final int pointerIndex = event.getActionIndex();
			final int pointerId = event.getPointerId(pointerIndex);
			if (pointerId == mActivePointerId) {
				// This was our active pointer going up,Choose a new
				// active pointer and adjust accordingly
				final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
				mLastTouchX = event.getX(newPointerIndex);
				mLastTouchY = event.getY(newPointerIndex);
				mActivePointerId = event.getPointerId(newPointerIndex);

			}
			break;
		}
		}
		return true;
	}

}
